package com.excilys.capicsoubank.validators;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.beans.factory.annotation.Autowired;

import com.excilys.capicsoubank.constraints.AccountIsValid;
import com.excilys.capicsoubank.services.AccountService;

/**
 * Validator pour l'annotation AccountIsValid.
 * 
 * @author "Grégory Coutant"
 * 
 */
public class AccountIsValidValidator implements
		ConstraintValidator<AccountIsValid, Integer> {

	@Autowired
	private AccountService accountService;

	@Override
	public void initialize(AccountIsValid arg0) {

	}

	@Override
	public boolean isValid(Integer accountId,
			ConstraintValidatorContext constaintContext) {

		if (accountId == null)
			return true;

		return accountService.getAccountById(accountId) != null;
	}
}
